/*
 * Copyright (c) 2021. Moses All Right Reserved
 */

package com.moses.design_patterns.proxy.dynamic_proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author Moses
 * @email shenhuohui@gmail.com
 * @date 2021/4/11 0:09
 */

//把 handler 和 获取代理类合为在一个类中
public class DynamicHandlerProxy<T> implements InvocationHandler {
    private T target = null;

    public static <T> T getProxy(T target){
        DynamicHandlerProxy handler = new DynamicHandlerProxy();
        handler.target = target;
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),handler);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return method.invoke(target, args);
    }
}
